<template lang="jade">
#account-other
  .personal-data
    form.weui-cells
      .weui-cell
        .weui-cell__bd
          input.weui-input(type='password', placeholder='旧密码', v-model='oldPassword')
      .weui-cell
        .weui-cell__bd
          input.weui-input(type='password', placeholder='新密码', v-model='newPassword')
      .weui-cell
        .weui-cell__bd
          input.weui-input(type='password', placeholder='重复新密码', v-model='confirmPassword')
    .weui-btn-area
      button.btn-blue.weui-btn.weui-btn_primary(@click.stop.prevent='update') 修改密码
</template>

<script>
import api from '../../../api-new'

export default {
  name: 'account-basic',
  data () {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    }
  },
  methods: {
    update () {
      if (this.oldPassword.length === 0 || this.newPassword.length === 0 || this.confirmPassword.length === 0) {
        window.alert('密码不能为空')
        return
      }
      if (this.newPassword !== this.confirmPassword) {
        window.alert('新密码两次输入不相同')
        return
      }
      // 修改密码
      const patronId = this.$cookie.get('uid')
      api.updatePassword({
        patronId,
        password: this.oldPassword,
        newPassword: this.newPassword
      }).then(response => {
        window.alert('修改成功')
      },
      error => {
        window.alert(error.message)
        console.error(error)
      })
    }
  }
}
</script>
